<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
include_once("../config.inc.php");

session_start();
$_LangPath = "10m15wager_insert";	//語言檔參數，務必放在最前面
$objMemberInfo = $_SESSION["Member_Info"];

if($objMemberInfo -> SuppleID == "" && $objMemberInfo -> WagerType == 0)include_once(__Member_Path."/logincheck.php");
include(__Language_Path."/selectlanguage_member.php");
include_once(__Modules_Path."/Wager.lib.php");
include_once(__Modules_Path."/WagerForm.lib.php");
include_once(__Common_Path."/Lotto.inc.php");
include_once(__Common_Path."/ExchangeRate.inc.php");
include_once(__Modules_Path."/LogRecord.lib.php");
include_once(__Modules_Path."/joinnumber.lib.php");
if($objMemberInfo -> isLock == '2'){
	$GLOBALS["errormessage"] = $_strStopWager;
	include("wager_error.php");
}

//Start Time
$startTime = Getmicrotime ();
$intLID = (int)$_data["LottoID"];
$intGID = (int)$_data["GambleID"];
$intLType = (int)$_arrLotto[$intLID]["LType"];
$intBT = (int)$_data["Bet_Type"];
$intNumber = (int)$_data["Number"];
if($intNumber != 1)$intNumber = 0;
//找出 Apoint
$intAN = (int)$_arrGLineNumToApointNum[$intLID][$intBT][$intNumber];

$intStakeMoney = (int)$_data["StakeMoney"];
$intNum 	= '0';
$_data["JoinNumber"] = trim($_data["JoinNumber"]);
$_data["OddsID"] = trim($_data["OddsID"]);
$_data["Odds"] = trim($_data["Odds"]);
$_data["CheckKey1"] = trim($_data["CheckKey1"]);
$_data["CheckKey2"] = trim($_data["CheckKey2"]);

if(__isConnectToOtherWeb == 1){
	$flMemERate = (empty($objMemberInfo -> ExRate))?1:$objMemberInfo -> ExRate;
}else{
	$flMemERate = (empty($_ECRate[$objMemberInfo -> ERID]["Rate"]))?1:$_ECRate[$objMemberInfo -> ERID]["Rate"];
}

//依下注種類，決定多少號為一組
$arrGroupCounter[1] = array(30 => 6, 31 => 5, 32 => 2, 33 => 3, 34 => 4, 35 => 2, 36 => 3, 37 => 4, 38 => 2, 39 => 3, 40 => 4, 41 => 2, 42 => 3, 43 => 4);
$arrGroupCounter[4] = array(15 => 1, 16 => 2, 17 => 3, 18 => 3, 19 => 4, 20 => 4, 21 => 5, 22 => 5, 23 => 5, 24 => 2, 25 => 2, 26 => 3, 27 => 4, 28 => 5, 30 => 2, 31 => 3, 32 => 4, 33 => 2, 34 => 3, 35 => 4);
$intGroupCounter = $arrGroupCounter[$intLType][$intBT];			//預設2個號碼為一組
if($intGroupCounter == null || $intGroupCounter <= 0){
	$Err_Title = $_str10M15WagerInsert["BetError"];
	$Err_Message = $_str10M15WagerInsert["BetAgain"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}

$arrAllJNumber 	= explode('|', $_data["JoinNumber"]);
$_arrMaxMinNum[1] = array("Max" => 49, "Min" => 1);
if($intLType == 1){
	if($intBT == 30)$_arrMaxMinNum[1] = ($arrAllJNumber[0] > 12)?array("Max" => 24, "Min" => 13):array("Max" => 12, "Min" => 1);
	elseif($intBT >= 32 && $intBT <= 37)$_arrMaxMinNum[1] = array("Max" => 12, "Min" => 1);
	elseif($intBT >= 38 && $intBT <= 43)$_arrMaxMinNum[1] = array("Max" => 9, "Min" => 0);
}elseif($intLType == 4){
	$_arrMaxMinNum[4] = array("Max" => 21, "Min" => 1);
	if($intBT >= 30 && $intBT <= 35)$_arrMaxMinNum[4] = array("Max" => 9, "Min" => 0);
}
$arrMaxMinNum = $_arrMaxMinNum[$intLType];

$JoinArray = null;
$strJoinNumberList = null;

$arrNum = array();
$arrOID = array();
$arrNumStake = array(); //每個號碼累積下注金額
if($intNumber == 0){
	//將 JoinNumber 切開後指給 JoinNumber 陣列
	array_unshift($arrAllJNumber, "NOUSE");
	unset ($arrAllJNumber[0]);
	foreach($arrAllJNumber as $k => $v){
		$intN = (int)$v;
		if($intN < $arrMaxMinNum["Min"] || $intN > $arrMaxMinNum["Max"]){
			$Err_Title = $_str10M15WagerInsert["BetError"];
			$Err_Message = $_str10M15WagerInsert["BetAgain"];
			$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
			include("wager_error.php");
		}
		$arrAllJNumber[$k] = sprintf("%02s", $intN);
		$arrNum[$intN] = $intN;
	}
	if(sizeof($arrAllJNumber) > 10 || sizeof($arrAllJNumber) != sizeof($arrNum)){
		$Err_Title = $_str10M15WagerInsert["BetError"];
		$Err_Message = $_str10M15WagerInsert["BetAgain"];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
	//取得連碼陣列
	$JoinArray = combination($arrAllJNumber, $intGroupCounter);
	$strChoiceCounter = sizeof($arrAllJNumber);
	$strJoinNumber = join(",", $arrAllJNumber);

//排碰
}else{
	if(sizeof($arrAllJNumber) != $intGroupCounter){
		$Err_Title = $_str10M15WagerInsert["BetError"];
		$Err_Message = $_str10M15WagerInsert["BetAgain"];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}

	$arrTip = array("a", "b", "c", "d", "e");

	$arrCTNo = array();
	$arrChoiceTNo = array();
	$arrChoiceList = array();
	$arrOrderList = array();
	$arrChoice = array();
	for($i = 0; $i < $intGroupCounter; $i++){
		$arrTmp = explode(",", $arrAllJNumber[$i]);
		$arrChk = array();
		foreach($arrTmp as $strN){
			$intN = (int)$strN;
			if($intN < $arrMaxMinNum["Min"] || $intN > $arrMaxMinNum["Max"]){
				$Err_Title = $_str10M15WagerInsert["BetError"];
				$Err_Message = $_str10M15WagerInsert["BetAgain"];
				$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
				include("wager_error.php");
			}
			$arrCTNo[$intN] = $i;
			$arrNum[$intN] = $intN;
			$arrChk[$intN] = $intN;
		}
		if(sizeof($arrTmp) != sizeof($arrChk)){
			//print_r($arrTmp);
			//print_r($arrNum);
			$Err_Title = $_str10M15WagerInsert["BetError"];
			$Err_Message = $_str10M15WagerInsert["BetAgain"];
			$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
			include("wager_error.php");
		}
	}
	foreach($arrCTNo as $intN => $intCNo){
		$arrChoiceTNo[$intCNo][] = sprintf("%02s", $intN);
	}
	for($i = 0; $i < $intGroupCounter; $i++){
		if(!is_array($arrChoiceTNo[$i])){
			$Err_Title = $_str10M15WagerInsert["BetError"];
			$Err_Message = $_str10M15WagerInsert["BetAgain"];
			include("wager_error.php");

		}elseif(sizeof($arrChoiceTNo[$i]) > 12){
			$Err_Title = $_str10M15WagerInsert["BetError"];
			$Err_Message = $_str10M15WagerInsert["BetAgain"];
			include("wager_error.php");
		}
		$arrChoiceList[$i] = '[<font class="left_jn_pg_choice_no">'.join(",", $arrChoiceTNo[$i]). '</font>]';
		$arrOrderList[$i] = implode(",", $arrChoiceTNo[$i]);
		$arrChoice[$i] = $arrTip[$i].implode(",".$arrTip[$i], $arrChoiceTNo[$i]);
	}
	$strJoinNumber = implode("x", $arrChoice);
	//取得下注總組數
	$JoinArray = combinationForLine($arrChoiceTNo, $intGroupCounter);
}

$arrJ_Number = array();
foreach ($JoinArray as $key => $value) {
	$arrJN	= explode('.', $value);
	sort($arrJN);
	//計算每個號碼累積注額
	foreach($arrJN as $intN){
		$arrNumStake[$intN] += $intStakeMoney / $intGroupCounter;
	}

	$JoinArray[$key] = implode('.', $arrJN);
	$strJ_Number = 'J'.sprintf("%012s", implode('', $arrJN));
	$arrJ_Number[$strJ_Number] = 1;
}

//取得連碼組數
$intChoiceGroup = count($JoinArray);

//將 OddsID 切開後指給 OddsID 陣列
$arrOddsID = explode(',', $_data["OddsID"]);
$arrOID = array();
foreach($arrOddsID as $strOddsID){
	$arrTOID = explode(":", $strOddsID);
	$arrOID[(int)$arrTOID[0]] = (int)$arrTOID[1];
}

//將 Odds 切開後指給 Odds 陣列
//$arrOdds = explode('|', $_data["Odds"]);

//設定總金額 - 金額乘組數
$_data["TotalStakeMoney"] 	= $intStakeMoney * $intChoiceGroup;

//檢查關鍵參數是否為空值
if(!$intLID || !$intLType || empty($intBT) || strlen($_data["CheckKey2"]) == 0 || strlen($_data["CheckKey1"]) == 0 || strlen($arrOID) == 0 || empty($_data["StakeMoney"]) || empty($intGID) || !is_array($JoinArray)){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_str10M15WagerInsert["BetError"];
	$Err_Message = $_str10M15WagerInsert["BetAgain"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}



//會員、代理商、總代理、股東退水質不得為空值
$arrComm[1] = $objMemberInfo -> arrCommission[$intLID][$intBT];				//會員退水值
$arrComm[2] = $objMemberInfo -> arrSubCommission[$intLID][$intBT];			//代理商退水值
$arrComm[3] = $objMemberInfo -> arrMainCommission[$intLID][$intBT];		//總代理退水值
$arrComm[4] = $objMemberInfo -> arrStockCommission[$intLID][$intBT];		//股東退水值
$arrComm[5] = $objMemberInfo -> arrDirectorCommission[$intLID][$intBT];	//總監退水值
$arrComm[6] = $objMemberInfo -> arrLayer6Commission[$intLID][$intBT];	//業務總監退水值
if(empty($arrComm[1]) || empty($arrComm[2]) || empty($arrComm[3]) || empty($arrComm[4]) || empty($arrComm[5]) || empty($arrComm[6])){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_str10M15WagerInsert["BetError"];
	$Err_Message = $_str10M15WagerInsert["CommissionErr"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}

include_once(__Common_Path."/limit/LottoLimit_".$intLID.".inc.php");
include_once(__Common_Path."/betparam/BetParam_".$intLID.".inc.php");

//移除 once 後面需要重新載入
$_arrMemStake = getWagerArrayGoldByDB($objMemberInfo -> ID, $objMemberInfo -> WagerType);
if($_arrMemStake === false){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$Err_Message = ($_strWagerInsert[$GLOBALS["errormessage"]] != "")?$_str10M15WagerInsert[$GLOBALS["errormessage"]]:$GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}

if(!is_array($_arrBetParam[$intBT][$intAN])){
	$Err_Message = "No Wager Relation Data!!";
	include("wager_error.php");
}
//===========================================================================//
//判斷最小單注或單場限額
$BetLimit = $objMemberInfo -> arrBetLimit[$intLID][$intBT];
$intEventLimit = $objMemberInfo -> arrEventLimit[$intLID][$intBT];

if($_arrLottoLimit[$intBT][0] > 0){
	$_arrLottoLimit[$intBT][0] = floor($_arrLottoLimit[$intBT][0] / $flMemERate);
	if($_arrLottoLimit[$intBT][0] < $intEventLimit)$intEventLimit = $_arrLottoLimit[$intBT][0];
}

if($_arrLottoLimit[$intBT][1] > 0){
	$_arrLottoLimit[$intBT][1] = floor($_arrLottoLimit[$intBT][1] / $flMemERate);
	if($_arrLottoLimit[$intBT][1] < $BetLimit)$BetLimit = $_arrLottoLimit[$intBT][1];
}

//公司單注限額
$Com_BetLimit = floor($_arrBetParam[$intBT][$intAN]["Com_BetLimit"] / ($objMemberInfo -> Layer6Scale*$flMemERate));
if($Com_BetLimit < $BetLimit)$BetLimit = $Com_BetLimit;

$intMinStake = floor($_arrBetParam[$intBT][$intAN]["MinStake"] / $flMemERate);
//下注金額不能小於最小下注金額
if($intStakeMoney < $intMinStake){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_str10M15WagerInsert["BetError"];
	$Err_Message = $_str10M15WagerInsert["NotUnderMinStake"]."(".number_format($intMinStake).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
//下注金額不能大於單注限額
if($intStakeMoney > $BetLimit){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_str10M15WagerInsert["BetError"];
	$Err_Message = $_str10M15WagerInsert["NotOverMaxStake"]."(".number_format($BetLimit).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
/*下注總金額不能大於單場限額的剩餘額度
$intEvent = $_arrMemStake[$intBT][$intGID][$intNum];
if($_data["TotalStakeMoney"] > ($intEventLimit - $intEvent)){
	$Err_Title = $_str10M15WagerInsert["BetError"];
	$Err_Message = $_str10M15WagerInsert["NotOverEvenLimit"];
	$Err_Message .= $_str10M15WagerInsert["EvenLimit"]."(".number_format($intEventLimit).")<br>";
	$Err_Message .= $_str10M15WagerInsert["ThisStake"]."(".number_format($intEvent).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
*/
//下注總金額不能大於信額額度
$intEvent = $_arrMemStake[$intBT][$intGID]["_SUM"];
$intSum = ($objMemberInfo -> WagerType == 0)?$_arrMemStake["_SUM"]:$intEvent;
if($_data["TotalStakeMoney"] > ($objMemberInfo -> CreditLine - $intSum)){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_str10M15WagerInsert["BetError"];
	$Err_Message = $_str10M15WagerInsert["NotOverCredit"];
	$Err_Message .= $_str10M15WagerInsert["MemCredit"]."(".number_format($objMemberInfo -> CreditLine).")<br>";
	$Err_Message .= $_str10M15WagerInsert["UsedCredit"]."(".number_format($intSum).")";
	$Err_Message .= "(".number_format($_data["TotalStakeMoney"]).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
//下注金額不能大於上層信額額度
if($objMemberInfo -> WagerType == 0){
	if(isOverParentCredit($_data["TotalStakeMoney"]*$flMemERate, $objMemberInfo -> SubID, $objMemberInfo -> SubCreditLine, $objMemberInfo -> MainID, $objMemberInfo -> MainCreditLine)){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		if($GLOBALS["errormessage"] == "")$Err_Message = $_str10M15WagerInsert["NotOverCredit"];
		$Err_Title = $_str10M15WagerInsert["BetError"];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
}

//剩餘信用額度
$intCreditSurplus = $objMemberInfo -> CreditLine - $_arrMemStake["_SUM"];

//===========================================================================//
//判斷是否啟動自動向上補貨
$isAutoSupple = false;
$arrAutoSuppleLimit = $objMemberInfo -> arrAutoSuppleLimit[$intLID][$intBT];
if(($objMemberInfo -> SubIsAutoSupple || $objMemberInfo -> MainIsAutoSupple || $objMemberInfo -> StockIsAutoSupple || $objMemberInfo -> DirectorIsAutoSupple || $objMemberInfo -> Layer6IsAutoSupple)){//$intLID == 1 && 
	if($_arrBetParam[$intBT][$intAN]["isAutoSupple"])$isAutoSupple = true;
}
if( ($arrAutoSuppleLimit[$objMemberInfo -> SubID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> SubID] == null) && 
	($arrAutoSuppleLimit[$objMemberInfo -> MainID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> MainID] == null) && 
	($arrAutoSuppleLimit[$objMemberInfo -> StockID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> StockID] == null) && 
	($arrAutoSuppleLimit[$objMemberInfo -> DirectorID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> DirectorID] == null) && 
	($arrAutoSuppleLimit[$objMemberInfo -> Layer6ID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> Layer6ID] == null)
)$isAutoSupple = false;

$arrAutoSQL = array();
$arrRealStake = array();
$arrLayerID = array(2 => $objMemberInfo -> SubID, 3 => $objMemberInfo -> MainID, 4 => $objMemberInfo -> StockID, 5 => $objMemberInfo -> DirectorID, 5 => $objMemberInfo -> Layer6ID);
if($_arrBetParam[$intBT][$intAN]["isAutoSupple"]){
	$strJNumber = implode(",", $JoinArray);
	$strJNumber = str_replace('.', '', $strJNumber);
	//代理商
	if($objMemberInfo -> SubIsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> SubID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> SubID] == null))$arrAutoSQL[] = "(select a.SubID as UserID, b.Number, SUM(IF(a.WagerType = 2, -1, 1*(1 - a.SubRate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.Bet_Type = ".$intBT." and a.SubID = ".$objMemberInfo -> SubID." and b.Number in (".$strJNumber.") group by b.Number)";

	//總代理
	if($objMemberInfo -> MainIsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> MainID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> MainID] == null))$arrAutoSQL[] = "(select a.MainID as UserID, b.Number, SUM(IF(a.WagerType = 3, -1, 1*(a.SubRate - a.MainRate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.Bet_Type = ".$intBT." and a.MainID = ".$objMemberInfo -> MainID." and b.Number in (".$strJNumber.") group by b.Number)";

	//股東
	if($objMemberInfo -> StockIsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> StockID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> StockID] == null))$arrAutoSQL[] = "(select a.StockID as UserID, b.Number, SUM(IF(a.WagerType = 4, -1, 1*(a.MainRate - a.StockRate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.Bet_Type = ".$intBT." and a.StockID = ".$objMemberInfo -> StockID." and b.Number in (".$strJNumber.") group by b.Number)";

	//總監
	if($objMemberInfo -> DirectorIsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> DirectorID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> DirectorID] == null))$arrAutoSQL[] = "(select a.DirectorID as UserID, b.Number, SUM(IF(a.WagerType = 5, -1, 1*(a.StockRate - a.DirectorRate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.Bet_Type = ".$intBT." and a.DirectorID = ".$objMemberInfo -> DirectorID." and b.Number in (".$strJNumber.") group by b.Number)";

	//業務總監
	if($objMemberInfo -> Layer6IsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> Layer6ID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> Layer6ID] == null))$arrAutoSQL[] = "(select a.Layer6ID as UserID, b.Number, SUM(IF(a.WagerType = 6, -1, 1*(a.DirectorRate - a.Layer6Rate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.Bet_Type = ".$intBT." and a.Layer6ID = ".$objMemberInfo -> Layer6ID." and b.Number in (".$strJNumber.") group by b.Number)";
}
//print_r($arrAutoSQL);
//exit();
//交易開始
PMA_mysql_query("BEGIN", $_ConnMaster);

$arrAutoSuppleGroup = array();
$arrRealStake = array();
if(sizeof($arrAutoSQL) > 0){
	$strAutoSQL = implode(" union ", $arrAutoSQL);
//print $strAutoSQL;
	if(!$RS = PMA_mysql_query($strAutoSQL, $$_Conn)){
		$Err_Message = "Find Data Error!!";
		include("wager_error.php");
	}

	while($Rows = PMA_mysql_fetch_object($RS)){
		$strNumber = 'J'.sprintf("%012s", $Rows -> Number);
		$arrRealStake[$strNumber][$Rows -> UserID] += $Rows -> RealStake;
		$arrJ_Number[$strNumber] = 1;
	}
//print_r($arrRealStake);
//print_r($arrJ_Number);
//exit();
	foreach($arrJ_Number as $strNumber => $v){
		foreach($arrLayerID as $intGroup => $intUID){
			$flRealStake = 0;
			$isLayerAutoSupple = false;
			$arrAutoSuppleGroupGold[$strNumber][$intGroup] = 0; //各階層自動補貨金額
			switch($intGroup){
				case 2:
					$flRealStake = $flMemERate*$intStakeMoney*(1 - $objMemberInfo -> SubScale);
					$isLayerAutoSupple = $objMemberInfo -> SubIsAutoSupple;
					break;

				case 3:
					$flRealStake = $flMemERate*$intStakeMoney*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale);
					$flRealStake += $arrAutoSuppleGroupGold[$strNumber][2]*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale);

					$isLayerAutoSupple = $objMemberInfo -> MainIsAutoSupple;
					break;

				case 4:
					$flRealStake = $flMemERate*$intStakeMoney*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale);
					//print $flRealStake."\n";
					$flRealStake += ($arrAutoSuppleGroupGold[$strNumber][2] + $arrAutoSuppleGroupGold[$strNumber][3])*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale);
					//print $flRealStake."\n";

					$isLayerAutoSupple = $objMemberInfo -> StockIsAutoSupple;
					break;

				case 5:
					$flRealStake = $flMemERate*$intStakeMoney*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale);
					$flRealStake += $arrAutoSuppleGroupGold[$strNumber][2]*($objMemberInfo -> LayerScale[2][4] - $objMemberInfo -> LayerScale[2][5]) + $arrAutoSuppleGroupGold[$strNumber][3]*($objMemberInfo -> LayerScale[3][4] - $objMemberInfo -> LayerScale[3][5]) + $arrAutoSuppleGroupGold[$strNumber][4]*($objMemberInfo -> LayerScale[4][4] - $objMemberInfo -> LayerScale[4][5]);

					$isLayerAutoSupple = $objMemberInfo -> DirectorIsAutoSupple;
					break;

				case 6:
					$flRealStake = $flMemERate*$intStakeMoney*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale);
					$flRealStake += $arrAutoSuppleGroupGold[$strNumber][2]*($objMemberInfo -> LayerScale[2][5] - $objMemberInfo -> LayerScale[2][6]) + $arrAutoSuppleGroupGold[$strNumber][3]*($objMemberInfo -> LayerScale[3][5] - $objMemberInfo -> LayerScale[3][6]) + $arrAutoSuppleGroupGold[$strNumber][4]*($objMemberInfo -> LayerScale[4][5] - $objMemberInfo -> LayerScale[4][6]) + $arrAutoSuppleGroupGold[$strNumber][5]*($objMemberInfo -> LayerScale[5][5] - $objMemberInfo -> LayerScale[5][6]);

					$isLayerAutoSupple = $objMemberInfo -> Layer6IsAutoSupple;
					break;
			}

			if($isLayerAutoSupple){
				if($arrAutoSuppleLimit[$intUID] < 0 || $arrAutoSuppleLimit[$intUID] == null)continue;

				if(($_arrBetParam[$intBT][$intAN]["AutoSuppleOverGold"] + $arrAutoSuppleLimit[$intUID]) <= ($arrRealStake[$strNumber][$intUID] + $flRealStake)){
					$arrAutoSuppleGold[$strNumber][$intUID] = $arrRealStake[$strNumber][$intUID] + $flRealStake - $arrAutoSuppleLimit[$intUID];
					$arrAutoSuppleGroup[$strNumber][$intUID] = $intGroup;
					$arrAutoSuppleGroupGold[$strNumber][$intGroup] = $arrAutoSuppleGold[$strNumber][$intUID];
				}
			}
		}
	}
}
/*print_r($arrRealStake);
print_r($arrAutoSuppleGold);
print_r($arrAutoSuppleLimit);
print_r($arrAutoSuppleGroup);
print_r($arrAutoSuppleGroupGold);
exit();*/
$arrCommission = array();
if($isAutoSupple && is_array($arrAutoSuppleGroup)){
	if(sizeof($arrAutoSuppleGroup) > 0){
		//找出代理商、總代理、股東、總監 A盤退水
		$strSQL = "select MemID, Commission from `bet_Commission` USE INDEX (MemID) where MemID in (".$objMemberInfo -> SubID.",".$objMemberInfo -> MainID.",".$objMemberInfo -> StockID.",".$objMemberInfo -> DirectorID.") and Gamble_Type = 1 and LottoID = ".$intLID." and Bet_Type in (".$intBT.")";
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$Err_Message = "Find Commission Data Error!!";
			//print $strSQL;
			include("wager_error.php");
		}

		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrCommission[$Rows -> MemID] = 1 - ($Rows -> Commission / 100);
		}
	}
}


//取得最新號碼賠率資料
$arrGData = getMultiWagerArrayData($intLID, $intLType, $intGID, $intBT, $arrNum, $arrOID);
if($arrGData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $_str10M15WagerInsert[$arrGData["Err_Message"]];
	include("wager_error.php");
}elseif($arrGData["isMessage"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $_str10M15WagerInsert[$arrGData["Message"]];
	include("wager_error.php");
}
//每個號碼的賠率
$arrNumOdds = array();
foreach($arrGData["NData"] as $intNum => $arrNData){
	$arrNumOdds[$intNum] = $arrNData["Odds"];
}

//======================================================================================
$flPayOffMoney = 0;
$intMaxEventLimit = $intEventLimit - $intStakeMoney;

//取得此會員此場已下注號碼的累積金額
$arrParlaysNumberEven = get_ParlaysNumberEven($objMemberInfo -> ID, $intGID, $intBT, $intMaxEventLimit);
if($arrParlaysNumberEven["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $_str10M15WagerInsert[$arrParlaysNumberEven["ErrorMessage"]];
	$Err_HiddenMessage = $arrParlaysNumberEven["ErrorHiddenMessage"];
	include("wager_error.php");
}

//$intMaxEventLimitGap = 最高差額 - 本次下注...
if($_KSysParam["isRealJumpWager"] == 0){
	$intMaxEventLimitGap = $_arrBetParam[$intBT][$intAN]["MaxStakeGap"] - $intStakeMoney;
}else{
	$intMaxEventLimitGap = $_arrBetParam[$intBT][$intAN]["MaxStakeGap"] - $intStakeMoney*$objMemberInfo -> DirectorScale;
}

//取得此 Bet_Type 的累積差額
$arrParlaysNumberEvenGap = get_ParlaysNumberEvenGap ($intGID, $intBT, $JoinArray);
if($arrParlaysNumberEvenGap["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $_str10M15WagerInsert[$arrParlaysNumberEvenGap["ErrorMessage"]];
	$Err_HiddenMessage = $arrParlaysNumberEvenGap["ErrorHiddenMessage"];
	include("wager_error.php");
}

//每組號碼的最小賠率
$arrJoinMinOdds = array();
$arrPayOffMoney = array();
$arrRealToday = array();
foreach ($JoinArray as $k => $strNum) {
	$strNumNoNode = str_replace('.', '', $strNum);
	$strNumNoNode = sprintf("%012s", $strNumNoNode);

	$arrN = explode('.', $strNum);

	//取得每組號碼最小賠率
	$flMinOdds = 99999999;
	foreach($arrN as $strN){
		$intN = (int)$strN;
		if($flMinOdds > $arrNumOdds[$intN]) $flMinOdds = $arrNumOdds[$intN];
	}
	foreach($arrN as $strN){
		$intN = (int)$strN;
		$arrPayOffMoney[$intN] += ($intStakeMoney / $intGroupCounter) * ($flMinOdds - 1);
	}

	$arrJoinMinOdds[$strNum] = $flMinOdds;
	$flPayOffMoney += $intStakeMoney * ($flMinOdds - 1); //加總每組號碼的派彩

	//比對此會員此場已下注號碼的累積金額是否已超過單場限額
	if ($arrParlaysNumberEven["Number"] != '0') {
		if (isset($arrParlaysNumberEven[$strNumNoNode])) {
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			$Err_Title = $_str10M15WagerInsert["BetError"];
			$Err_Message = $_str10M15WagerInsert["OverEvenLimit"].$strNum.'<br />';
			$Err_HiddenMessage	= '<br />'. $_str10M15WagerInsert["EvenLimits"]."(".number_format($intEventLimit).")<br />";
			$Err_HiddenMessage	.= $_str10M15WagerInsert["ThisStake"]."(".number_format($arrParlaysNumberEven[$strNumNoNode] + $intStakeMoney).")";
			$Err_HiddenMessage	.= "\nLine:".__LINE__.",\nPage:".__FILE__;
			include("wager_error.php");
		}
	}

	//比對此會員此場已下注號碼的累積金額是否已超過單場限額
	if ($arrParlaysNumberEvenGap["Number"] != '0') {
		//資料庫有值有值而且超過最高差額 or 資料庫無值但是下注金額已超過最高差額
		if ((isset($arrParlaysNumberEvenGap[$strNumNoNode]) && ($arrParlaysNumberEvenGap[$strNumNoNode] > $intMaxEventLimitGap)) || (!isset($arrParlaysNumberEvenGap[$strNumNoNode]) && $intMaxEventLimitGap < 0)) {
			PMA_mysql_query("ROLLBACK", $_ConnMaster);

			$strLog = "GID: ".$intGID.", LID: ".$intLID.", BT: ".$intBT.", LType: ".$intLType.", Num: ".$strNum;
			if(!StopBetTodayGame($intGID, $intLID, $intBT, $intLType, 1, str_replace('.', ',', $strNum))){
				$strLog .= "\n".$GLOBALS["errormessage"]."\n".$GLOBALS["errorhidden"];
				AddData_Log($objMemberInfo -> ID, "停押失敗 ", $arrBetData["LogData"]);
			}else{
				AddData_Log($objMemberInfo -> ID, "停押成功 ", $strLog);
			}

			$Err_Title = $_str10M15WagerInsert["BetError"];
			$Err_Message = $_str10M15WagerInsert["OverEvenGapLimit"].$strNum.'<br />';
			$Err_HiddenMessage	= '<br>'. $_str10M15WagerInsert["EvenGapLimits"]."(".number_format($intMaxEventLimitGap).")<br>";
			$Err_HiddenMessage	.= $_str10M15WagerInsert["ThisStake"]."(".number_format($arrParlaysNumberEvenGap[$strNumNoNode]).")";
			$Err_HiddenMessage	.= "\nLine:".__LINE__.",\nPage:".__FILE__;
			include("wager_error.php");
		}
	}
}
//======================================================================================
//print_r($arrJoinMinOdds);

//組合成功顯示可贏金額字串
//eval("\$PayOffValue = \"$PayOffValue\";");
//eval("\$strJoinDocTitle = \"$_str10M15WagerInsert[JoinDocTitle]\";");

//組合陣列 for bet_BetListContent 用
$arrWCont["GameDate_x"] = $arrGData["GData"]["GameDate"];
$arrWCont["GambleType"] = $objMemberInfo -> GambleType;
$arrWCont["MemAccount_x"] = $objMemberInfo -> Account;
$arrWCont["isBlack"] = $objMemberInfo -> isBlack;
$arrWCont["SuppleID_x"] = $intSuppleWagerID."";

$arrWList["LottoID"] = $arrGData["GData"]["LottoID"];
$arrWList["LType"] = $intLType;
$arrWList["Bet_Type"] = $intBT;
$arrWList["GambleID"] = $arrGData["GData"]["GambleID"];
$arrWList["Number"] = $intNumber;

$arrWList["PayupDate_x"] 	= $arrGData["GData"]["PayupDate"];
$arrWList["MemPayOff"] = $flPayOffMoney; //$flMemPayOff*$intChoiceGroup;
$arrWList["MemID"] = $objMemberInfo -> ID;
$arrWList["MemExchangeRate"] = $flMemERate;
$arrWList["MemCurrency"] = $objMemberInfo -> ERID;
$arrWList["ExRecordID"] = $objMemberInfo -> ExRecordID;
$arrWList["MemStake"] = $intStakeMoney*$intChoiceGroup;
$arrWList["MemCommission"] = 1 - ($arrComm[1] / 100);
$arrWList["SubID"] = $objMemberInfo -> SubID;
$arrWList["SubCommission"] = 1 - ($arrComm[2] / 100);
$arrWList["SubRate"] = $objMemberInfo -> SubScale;
$arrWList["MainID"] = $objMemberInfo -> MainID;
$arrWList["MainCommission"] = 1 - ($arrComm[3] / 100);
$arrWList["MainRate"] = $objMemberInfo -> MainScale;
$arrWList["StockID"] = $objMemberInfo -> StockID;
$arrWList["StockCommission"] 	= 1 - ($arrComm[4] / 100);
$arrWList["StockRate"] = $objMemberInfo -> StockScale;
$arrWList["DirectorID"] = $objMemberInfo -> DirectorID;
$arrWList["DirectorCommission"] 	= 1 - ($arrComm[5] / 100);
$arrWList["DirectorRate"] = $objMemberInfo -> DirectorScale;
$arrWList["Layer6ID"] = $objMemberInfo -> Layer6ID;
$arrWList["Layer6Commission"] = 1 - ($arrComm[6] / 100);
$arrWList["Layer6Rate"] = $objMemberInfo -> Layer6Scale;
if($objMemberInfo -> WagerType >= 2)$arrWList["WagerType"] = $objMemberInfo -> WagerType;

if($arrWList["StockCommission"] < 0 || $arrWList["StockCommission"] >= 0.3 || $arrWList["StockCommission"] < $arrWList["MainCommission"] || $arrWList["MainCommission"] < $arrWList["SubCommission"] || $arrWList["SubCommission"] < $arrWList["MemCommission"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$Err_Message = "Commission Error!!";
	//print_r($arrWList);
	include("wager_error.php");
}


//組合陣列 for bet_BetListContent 用
$arrWCont["checkkey_x"]	= str_replace("0.", $_data["CheckKey2"]."-", $_data["CheckKey1"]);

$arrWCont["Content_x"] = getWagerConditions($intLType, $intBT, $intNumber, null, $arrGData["GData"]["SerialNo"], null, $arrAllJNumber, $intChoiceGroup, $arrNumOdds);
$strChoiceNumberList = WagerFormTrans($intLID, $intBT, $arrWCont["Content_x"])."<br /><br />".$strChoiceNumberList;
$arrWCont["OneStake"] = $intStakeMoney;
$arrWCont["JoinNumber_x"] = $strJoinNumber;

//寫入注單
$arrBetData = InsertWagerToDB($arrWCont, $arrWList);
if($arrBetData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $_str10M15WagerInsert[$arrBetData["ErrorMessage"]];
	$Err_HiddenMessage = $arrBetData["ErrorHiddenMessage"];
	include("wager_error.php");
}
$intWagerID = $arrBetData["BetID"];
$strJoinDocTitle = $_str10M15WagerInsert["WagerNo"].$intWagerID;

//寫入下注號碼 ParlaysNumberEven ... 是 Even 喔
$arrBetParlaysEvenData = InsertParlaysNumberEvenToDB($arrWList["MemID"], $arrWList["SubID"], $arrWList["MainID"], $arrWList["StockID"], $arrWList["DirectorID"], $arrWList["GambleID"], $arrWList["Bet_Type"], $intWagerID, $JoinArray, $intStakeMoney, 0, 0, $arrJoinMinOdds);
if($arrBetParlaysEvenData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $_str10M15WagerInsert[$arrBetParlaysEvenData["ErrorMessage"]];
	$Err_HiddenMessage = $arrBetParlaysEvenData["ErrorHiddenMessage"];
	include("wager_error.php");
}

//自動補貨
$arrPlusStake = array();
$j = 0;
$intNumber = 0;
foreach($arrAutoSuppleGroup as $strNum => $arrAutoSG){
	$arrJoinNumber = array();
	$arrNOdds = array();

	for($i = ((6 - $intGroupCounter)*2 + 1); $i < 12; $i += 2){
		$strN = substr($strNum, $i, 2);
		$arrJoinNumber[] = $strN;
		$arrNOdds[$strN] = $arrNumOdds[(int)$strN];
	}
	$strJoinNumber = implode(',', $arrJoinNumber);
	$strJoinArray = implode('.', $arrJoinNumber);
	$flTOdds = $arrJoinMinOdds[$strJoinArray];

	foreach($arrAutoSG as $intUID => $intGroup){
		$arrWListTmp = $arrWList;
		$arrWListTmp["MemExchangeRate"] = 1;
		$arrWListTmp["MemCurrency"] = 1;
		$arrWListTmp["Number"] = 0;
		$arrWListTmp["MemStake"] = $arrAutoSuppleGold[$strNum][$intUID];
		$arrWListTmp["MemPayOff"] = $arrAutoSuppleGold[$strNum][$intUID]*($flTOdds - 1);
		$arrWListTmp["WagerType"] = $intGroup;

		$arrWContTmp = $arrWCont;
		//取得最新號碼賠率資料(1個賠率)
		$arrWContTmp["Content_x"] = getWagerConditions($intLType, $intBT, $intNumber, $flTOdds, $arrGData["GData"]["SerialNo"], '', $arrJoinNumber, 1, $arrNOdds);

		$strCheckKey = str_replace("0.", $_data["CheckKey2"].$j.$intGroup.$intBT.$intNum."-", $_data["CheckKey1"]);
		$arrWContTmp["checkkey_x"] = $strCheckKey;
		$arrWContTmp["Content_x"] = $arrWContTmp["Content_x"]." (Auto)";
		$arrWContTmp["JoinNumber_x"] = $strJoinNumber;
		$arrWContTmp["OneStake"] = $arrAutoSuppleGold[$strNum][$intUID];

		$arrWListTmp["Layer6Commission"] = $arrCommission[$objMemberInfo -> Layer6ID];
		$arrWListTmp["DirectorCommission"] = $arrCommission[$objMemberInfo -> DirectorID];
		$arrWListTmp["StockCommission"] = $arrCommission[$objMemberInfo -> StockID];
		$arrWListTmp["MainCommission"] = $arrCommission[$objMemberInfo -> MainID];
		$arrWListTmp["SubCommission"] = $arrCommission[$objMemberInfo -> SubID];

		$arrLayerScale = $objMemberInfo -> LayerScale[$intGroup];
		$arrWListTmp["SubRate"] = $arrLayerScale[2];
		$arrWListTmp["MainRate"] = $arrLayerScale[3];
		$arrWListTmp["StockRate"] = $arrLayerScale[4];
		$arrWListTmp["DirectorRate"] = $arrLayerScale[5];
		$arrWListTmp["Layer6Rate"] = $arrLayerScale[6];
		switch($intGroup){
			case 6:
				$arrWListTmp["DirectorID"] = $intUID;
				$arrWListTmp["DirectorCommission"] = $arrCommission[$intUID];
			case 5:
				$arrWListTmp["StockID"] = $intUID;
				$arrWListTmp["StockCommission"] = $arrCommission[$intUID];
			case 4:
				$arrWListTmp["MainID"] = $intUID;
				$arrWListTmp["MainCommission"] = $arrCommission[$intUID];
			case 3:
				$arrWListTmp["SubID"] = $intUID;
				$arrWListTmp["SubCommission"] = $arrCommission[$intUID];
			case 2:
				$arrWListTmp["MemID"] = $intUID;
				$arrWListTmp["MemCommission"] = $arrCommission[$intUID];
				break;
		}
		$arrWContTmp["MemAccount_x"] = $objMemberInfo -> LayerAcc[$intGroup];

		foreach($arrJoinNumber as $strN){
			$intN = (int)$strN;
			switch($intGroup){
				case 2:
					$arrRealToday[$intN][$objMemberInfo -> MainID]["W"] += 1;
					$arrRealToday[$intN][$objMemberInfo -> MainID]["S"] += $arrWListTmp["MemStake"] / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> MainID]["P"] += $arrWListTmp["MemPayOff"] / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> MainID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["SubCommission"]*($arrLayerScale[2] - $arrLayerScale[3]) / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> MainID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[2] - $arrLayerScale[3]) / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> MainID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[2] - $arrLayerScale[3]) / $intGroupCounter;
				case 3:
					$arrRealToday[$intN][$objMemberInfo -> StockID]["W"] += 1;
					$arrRealToday[$intN][$objMemberInfo -> StockID]["S"] += $arrWListTmp["MemStake"] / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> StockID]["P"] += $arrWListTmp["MemPayOff"] / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> StockID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["MainCommission"]*($arrLayerScale[3] - $arrLayerScale[4]) / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> StockID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[3] - $arrLayerScale[4]) / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> StockID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[3] - $arrLayerScale[4]) / $intGroupCounter;
				case 4:
					$arrRealToday[$intN][$objMemberInfo -> DirectorID]["W"] += 1;
					$arrRealToday[$intN][$objMemberInfo -> DirectorID]["S"] += $arrWListTmp["MemStake"] / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> DirectorID]["P"] += $arrWListTmp["MemPayOff"] / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> DirectorID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["StockCommission"]*($arrLayerScale[4] - $arrLayerScale[5]) / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> DirectorID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[4] - $arrLayerScale[5]) / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> DirectorID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[4] - $arrLayerScale[5]) / $intGroupCounter;
				case 5:
					$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["W"] += 1;
					$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["S"] += $arrWListTmp["MemStake"] / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["P"] += $arrWListTmp["MemPayOff"] / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["StockCommission"]*($arrLayerScale[5] - $arrLayerScale[6]) / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[5] - $arrLayerScale[6]) / $intGroupCounter;
					$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[5] - $arrLayerScale[6]) / $intGroupCounter;
				case 6:
					break;
			}
			$arrRealToday[$intN][$intUID]["SW"] += 1;
			$arrRealToday[$intN][$intUID]["SS"] += $arrWListTmp["MemStake"] / $intGroupCounter;
			$arrRealToday[$intN][$intUID]["SP"] += $arrWListTmp["MemPayOff"] / $intGroupCounter;
			$arrRealToday[$intN][$intUID]["SC"] += $arrWListTmp["MemCommission"]*$arrWListTmp["MemStake"] / $intGroupCounter;

			$arrPlusStake[$intN]["Stake"] +=  $arrWListTmp["MemStake"] / $intGroupCounter;
			$arrPlusStake[$intN]["RealStake"] +=  $arrWListTmp["MemStake"]*$arrWListTmp["DirectorRate"] / $intGroupCounter;
			$arrPlusStake[$intN]["PayOff"] +=  $arrWListTmp["MemPayOff"] / $intGroupCounter;
			$arrPlusStake[$intN]["RealPayOff"] +=  $arrWListTmp["MemPayOff"]*$arrWListTmp["DirectorRate"] / $intGroupCounter;
		}

		//寫入自動補貨注單
		$arrAutoSuppleBetData = InsertWagerToDB($arrWContTmp, $arrWListTmp);
		if($arrAutoSuppleBetData["isError"]){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			$Err_Message = $arrAutoSuppleBetData["ErrorMessage"]."1";
			$Err_HiddenMessage = $arrAutoSuppleBetData["ErrorHiddenMessage"];
			include("wager_error.php");
		}
		$j++;
		$intAutoSuppleWagerID = $arrAutoSuppleBetData["BetID"];

		//寫入下注號碼 ParlaysNumberEven ... 是 Even 喔
		$arrBetParlaysEvenData = InsertParlaysNumberEvenToDB($arrWListTmp["MemID"], $arrWListTmp["SubID"], $arrWListTmp["MainID"], $arrWListTmp["StockID"], $arrWListTmp["DirectorID"], $arrWListTmp["GambleID"], $arrWListTmp["Bet_Type"], $intAutoSuppleWagerID, array($strJoinArray), $arrWListTmp["MemStake"], 0, 0, array($strJoinArray => $flTOdds));
		if($arrBetParlaysEvenData["isError"]){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			$endTime = Getmicrotime ();
			$RunTime = 'RunTime:'. ($endTime - $startTime);
			$Err_Message = $_str10M15WagerInsert[$arrBetParlaysEvenData["ErrorMessage"]];
			$Err_HiddenMessage = $arrBetParlaysEvenData["ErrorHiddenMessage"];
			include("wager_error.php");
		}
	}
}

//下單計數
$StartCounter = $intChoiceGroup;
//押碼跳動 - 更新 TodayGame 壓碼不會跳動
$arrGData_Tmp = $arrGData["GData"];
foreach($arrNumStake as $intTN => $intStake){
	$intN = (int)$intTN;
	foreach($arrGData["NData"][$intN] as $k => $v){
		$arrGData_Tmp[$k] = $v;
	}
	$arrJumpData = WagerJump($arrGData_Tmp, $intStake, $arrPayOffMoney[$intN], $_arrBetParam[$intBT][$intAN], $_arrGambleTypeAllOdds[$intBT][0], $arrWList["MemExchangeRate"]*$arrWList["DirectorRate"], $arrWList["DirectorCommission"], $arrPlusStake[$intN]);
	//print_r($arrGData_Tmp);
	//print_r($arrPlusStake[$intN]);
	if($arrJumpData["isError"]){
		//print_r($arrGData["NData"]);
		//print $intN."\nQQQ";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$endTime = Getmicrotime ();
		$RunTime = 'RunTime:'. ($endTime - $startTime);
		$Err_Message = $arrBetData["ErrorMessage"].$GLOBALS["errormessage"];
		$Err_HiddenMessage = $arrBetData["ErrorHiddenMessage"].$GLOBALS["errorhidden"];
		include("wager_error.php");
	}

	switch($objMemberInfo -> WagerType){
		case 0:
			$arrRealToday[$intN][$objMemberInfo -> SubID]["W"] += 1;
			$arrRealToday[$intN][$objMemberInfo -> SubID]["S"] += $intStake;
			$arrRealToday[$intN][$objMemberInfo -> SubID]["P"] += $intStake + $arrPayOffMoney[$intN];
			$arrRealToday[$intN][$objMemberInfo -> SubID]["C"] += $intStake*$arrWList["MemCommission"]*$flMemERate*(1 - $objMemberInfo -> SubScale);
			$arrRealToday[$intN][$objMemberInfo -> SubID]["RS"] += $intStake*$flMemERate*(1 - $objMemberInfo -> SubScale);
			$arrRealToday[$intN][$objMemberInfo -> SubID]["RP"] += ($intStake + $arrPayOffMoney[$intN])*$flMemERate*(1 - $objMemberInfo -> SubScale);
		case 2:
			$arrRealToday[$intN][$objMemberInfo -> MainID]["W"] += 1;
			$arrRealToday[$intN][$objMemberInfo -> MainID]["S"] += $intStake;
			$arrRealToday[$intN][$objMemberInfo -> MainID]["P"] += $intStake + $arrPayOffMoney[$intN];
			$arrRealToday[$intN][$objMemberInfo -> MainID]["C"] += $intStake*$arrWList["MemCommission"]*$flMemERate*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale);
			$arrRealToday[$intN][$objMemberInfo -> MainID]["RS"] += $intStake*$flMemERate*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale);
			$arrRealToday[$intN][$objMemberInfo -> MainID]["RP"] += ($intStake + $arrPayOffMoney[$intN])*$flMemERate*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale);
		case 3:
			$arrRealToday[$intN][$objMemberInfo -> StockID]["W"] += 1;
			$arrRealToday[$intN][$objMemberInfo -> StockID]["S"] += $intStake;
			$arrRealToday[$intN][$objMemberInfo -> StockID]["P"] += $intStake + $arrPayOffMoney[$intN];
			$arrRealToday[$intN][$objMemberInfo -> StockID]["C"] += $intStake*$arrWList["MemCommission"]*$flMemERate*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale);
			$arrRealToday[$intN][$objMemberInfo -> StockID]["RS"] += $intStake*$flMemERate*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale);
			$arrRealToday[$intN][$objMemberInfo -> StockID]["RP"] += ($intStake + $arrPayOffMoney[$intN])*$flMemERate*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale);
		case 4:
			$arrRealToday[$intN][$objMemberInfo -> DirectorID]["W"] += 1;
			$arrRealToday[$intN][$objMemberInfo -> DirectorID]["S"] += $intStake;
			$arrRealToday[$intN][$objMemberInfo -> DirectorID]["P"] += $intStake + $arrPayOffMoney[$intN];
			$arrRealToday[$intN][$objMemberInfo -> DirectorID]["C"] += $intStake*$arrWList["MemCommission"]*$flMemERate*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale);
			$arrRealToday[$intN][$objMemberInfo -> DirectorID]["RS"] += $intStake*$flMemERate*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale);
			$arrRealToday[$intN][$objMemberInfo -> DirectorID]["RP"] += ($intStake + $arrPayOffMoney[$intN])*$flMemERate*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale);
		case 5:
			$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["W"] += 1;
			$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["S"] += $intStake;
			$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["P"] += $intStake + $arrPayOffMoney[$intN];
			$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["C"] += $intStake*$arrWList["MemCommission"]*$flMemERate*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale);
			$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["RS"] += $intStake*$flMemERate*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale);
			$arrRealToday[$intN][$objMemberInfo -> Layer6ID]["RP"] += ($intStake + $arrPayOffMoney[$intN])*$flMemERate*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale);
		case 6:
			break;
	}
	if($objMemberInfo -> WagerType > 0){
		$arrWagerTypeUID = array(2 => $objMemberInfo -> SubID, 3 => $objMemberInfo -> MainID, 4 => $objMemberInfo -> StockID, 5 => $objMemberInfo -> DirectorID, 6 => $objMemberInfo -> Layer6ID);
		$intUID = $arrWagerTypeUID[$objMemberInfo -> WagerType];
		$arrRealToday[$intN][$intUID]["SW"] += 1;
		$arrRealToday[$intN][$intUID]["SS"] += $intStake;
		$arrRealToday[$intN][$intUID]["SP"] += $intStake + $arrPayOffMoney[$intN];
		$arrRealToday[$intN][$intUID]["SC"] += $intStake*$arrWList["MemCommission"];
	}
}

if(!setCountWagerForAllLayers($intLID, $intGID, array($intBT => $arrRealToday))){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$Err_Message = ($_strWagerInsert[$GLOBALS["errormessage"]] != "")?$_strWagerInsert[$GLOBALS["errormessage"]]:$GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"]."6";
	include("wager_error.php");
}
//print_r($arrPlusStake);
//exit();

//產生累積注額陣列
if(!getWagerArrayGold($objMemberInfo -> ID)){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}

//產生下注列表
$intLoopTime = 0;
foreach ($JoinArray as $strNum) {
	$strNumTip = $strNum;
	if(($intLType == 1 && $intBT >= 38 && $intBT <= 43) || ($intLType == 4 && $intBT >= 30 && $intBT <= 35)){
		$arrN = explode(".", $strNum);
		foreach($arrN as $k => $strN){
			$arrN[$k] = (int)$strN;
		}
		$strNumTip = implode(",", $arrN).' ('.$_strSame_String["Mantissa"].')';
	}
	$intLoopTime++;
	$strJoinNumberList .= '
	  			<tr class=left_jn_pg_choice_no>
	    			  <td class=left_jn_pg_choice_id>'. (string)$intLoopTime.'</td>
	    			  <td class=left_jn_pg_choice_no>'. $strNumTip.'</td>
	    			  <td class=left_jn_pg_choice_cash>'. $intStakeMoney .'</td>
					  <td class=left_jn_pg_choice_cash>'. $arrJoinMinOdds[$strNum].'</td>
	  			</tr>
	  		    '."\n";//$arrSucc[BetID][$LoopTime]
}

//交易完成
PMA_mysql_query("COMMIT", $_ConnMaster);

AddData_Log($objMemberInfo -> ID,"下注成功 ", $arrBetData["LogData"]);
$Err_Message = "Success!!";
@mysql_close($$_Conn);
include("10m15wager_success.php");
?>